var app = getApp();
Page({
  data: {
    nickName: "",
    department: "",
    Departments: [],
    studentNum: "",
    class: "",
    campusCardId: "",
    hide: true,
    placeholder: "",
    content: "",//用来清除输入框
    nowItem: "",//用来判断哪个函数调用输入框
    temValue: { item: "", value: "" },//监听时的临时数据
  }, onLoad() {
    wx.cloud.callFunction({name:'function',data:{type:"get",table:"department",where:{ _id:'be7fb3985ff745d70322e65c08741978'}}}).then(res=>{this.setData({Departments: res.result.user.data[0].departments})})
    var data = app.globalData.userInfo
    this.setData({campusCardId: data.campusCardId ? data.campusCardId : "",class: data.class ? data.class : "",department: data.department?data.department : "",studentNum: data.studentNum ? data.studentNum : "",nickName: data.nickName})
  }, update() {
    app.globalData.userInfo.campusCardId = this.data.campusCardId
    app.globalData.userInfo.class = this.data.class
    app.globalData.userInfo.department = this.data.department
    app.globalData.userInfo.studentNum = this.data.studentNum
    wx.cloud.callFunction({ name: 'function', data: { type: "update", table: "user", where: { openid: app.globalData.database.openId }, update: { data: {userInfo:{campusCardId: this.data.campusCardId, class: this.data.class, department: this.data.department, studentNum: this.data.studentNum}} } } }).then(res => {wx.navigateBack({ delta: 0 })})
  }, editStudentNum() {
    this.setData({ hide: false, nowItem: "editStudentNum", placeholder: "请输入学号" })
  }, editClass() {
    this.setData({ hide: false, nowItem: "editClass", placeholder: "例如:B1905" })
  }, editCampusCardId() {
    this.setData({ hide: false, nowItem: "editCampusCardId", placeholder: "校园卡号" })
  }, inputToastBinder(a) {
    this.setData({
      temValue: { value: a.detail.value, item: a.currentTarget.dataset.name }
    })
  }, inputToastCancel() {
    this.setData({ hide: true, content: "" })
  }, inputToastConfirm() {
    this.setData({
      hide: true, content: ""
    })
    if (this.data.temValue.item == "editDepartment") {
      this.setData({ department: this.data.temValue.value })
    }
    if (this.data.temValue.item == "editStudentNum") {
      this.setData({ studentNum: this.data.temValue.value })
    }
    if (this.data.temValue.item == "editClass") {
      this.setData({ class: this.data.temValue.value })
    }
    if (this.data.temValue.item == "editCampusCardId") {
      this.setData({ campusCardId: this.data.temValue.value })
    }
  }, bindPickerChange: function (e) {
    this.setData({ department: this.data.Departments[e.detail.value] })
  }
})